package com.gitee.hljdrl.uikit.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Looper;
import android.util.TypedValue;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

import com.gitee.hljdrl.uikit.R;
import com.gitee.hljdrl.uikit.UIKit;
import com.gitee.hljdrl.uikit.UIKitOption;
import com.gitee.hljdrl.uikit.basic.UIKitOptionEvent;


public class Util {

    /********防止按钮连续点击********/
    private static long mLastClickTime;

    private static synchronized boolean isFastClick() {
        long time = System.currentTimeMillis();
        if (time - mLastClickTime < UIKitOption.getInstance().getOptionAsLong(UIKitOptionEvent.OPTION_DOUBLE_CLICK_TIME,1000)) {
            return true;
        }
        mLastClickTime = time;
        return false;
    }

    public static boolean isUIThread() {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            // UI线程
            return true;
        }
        return false;
    }

    public static int dp2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

    public static int screenHeight() {
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }


    public static void dismissDialog(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            try {
                dialog.dismiss();
            } catch (Exception ex) {
            }
        }
    }

    public static boolean isDestroyed(Activity _activity) {
        if (_activity == null || _activity.isDestroyed() || _activity.isFinishing()) {
            return true;
        }
        return false;
    }


    public static int getColorPrimary(Activity activity) {
        TypedValue typedValue = new TypedValue();
        activity.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        return typedValue.data;

    }

    public static int getDarkColorPrimary(Activity activity) {
        TypedValue typedValue = new TypedValue();
        activity.getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
        return typedValue.data;

    }

    public static int getColorAccent(Activity activity) {
        TypedValue typedValue = new TypedValue();
        activity.getTheme().resolveAttribute(R.attr.colorAccent, typedValue, true);
        return typedValue.data;

    }



    public static Drawable createRoundDrawable(@ColorInt int color, float radius, Rect padding) {
        float[] outerRadii = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii, null, null);
        ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
        shapeDrawable.getPaint().setColor(color);
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        if(padding!=null) {
            shapeDrawable.setPadding(padding);
        }
        return shapeDrawable;
    }


    public static Drawable tint9PatchDrawableFrame(Context context, @DrawableRes int bg, @ColorInt int tintColor) {
        Drawable toastDrawable = ContextCompat.getDrawable(context, bg);
        if (tintColor != 0 && toastDrawable != null) {
            toastDrawable.setColorFilter(new PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN));
        }
        return toastDrawable;
    }
}
